package com.example.cqcet_demo.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.cqcet_demo.R;
import com.example.cqcet_demo.bean.Page;
import com.example.cqcet_demo.uitls.AnimationDailog;
import com.example.cqcet_demo.uitls.CloseActivity;
import com.example.cqcet_demo.uitls.MyCode;
import com.example.cqcet_demo.uitls.OkHttpUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {


    private long firstTime = 0;
    private EditText userName;
    private EditText passWord;
    private EditText code;
    private ImageView loginCode;
    private Button btn_login;
    private OkHttpClient okHttpClient;
    private Context context;
    private ImageView imageView;
    private Dialog myDialog;
    private boolean flag;
    private List<Page> items;


    Map<String,String> postDatas = new HashMap<>(); //post提交数据
    private final String LOGIN_URL = "http://ecard.cqcet.edu.cn/default.aspx"; //首页



    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0x1234:
                    loginCode.setImageBitmap((Bitmap) msg.obj);
                    flag = true;
                default:
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        setContentView(R.layout.activity_login);
        CloseActivity.add(LoginActivity.this);

        initView();

        btn_login.setOnClickListener(new MyOnClickListener());
        imageView.setOnClickListener(new MyOnClickListener());

        //OkHttpUtil.resetCookies();
        okHttpClient = OkHttpUtil.getInstance();
        getImage();

    }

    private void initView() {
        userName = (EditText)findViewById(R.id.et_inputUsername);
        passWord = (EditText) findViewById(R.id.et_inputPassword);
        code = (EditText) findViewById(R.id.et_inputCode);
        loginCode = (ImageView)findViewById(R.id.iv_loginCode);
        btn_login = (Button)findViewById(R.id.btn_login);
        imageView = (ImageView)findViewById(R.id.iv_reload);
        items = new ArrayList<Page>();

        SharedPreferences pref =getSharedPreferences("userInfoData", MODE_PRIVATE);
        userName.setText(pref.getString("userName",""));
        passWord.setText(pref.getString("passWord",""));

        myDialog= AnimationDailog.createAnimationDailog(LoginActivity.this,"正在登陆中...");

    }

    class MyOnClickListener implements View.OnClickListener{
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_login:
                    if("".equals(userName.getText().toString())) {
                        Toast.makeText(context, "用户名不能为空,请重新输入", Toast.LENGTH_SHORT).show();
                    }else if("".equals(passWord.getText().toString())) {
                        Toast.makeText(context, "密码用户名不能为空,请重新输入", Toast.LENGTH_SHORT).show();
                    }else if("".equals(code.getText().toString())) {
                        Toast.makeText(context, "验证码不能为空,请重新输入", Toast.LENGTH_SHORT).show();
                    }else{
                        postDatas.put("UserLogin:txtSure",code.getText().toString());
                        postDatas.put("UserLogin:txtUser",userName.getText().toString());
                        postDatas.put("UserLogin:txtPwd",passWord.getText().toString());
                        if(code.getText().toString().equals(MyCode.getInstance().getCode())) {
                            myDialog.show();
                            login();
                        }else{
                            Toast.makeText(context, "验证码输入错误，请重试", Toast.LENGTH_SHORT).show();
                        }
                    }
                    break;
                case R.id.iv_reload:
                    if(flag) {
                        flag = false;
                        getImage();
                    }
                    break;
            }
        }
    }

    private void login() {

            postDatas.put("__EVENTTARGET","");
            postDatas.put("__LASTFOCUS","");
            postDatas.put("__EVENTARGUMENT","");
            postDatas.put("UserLogin:ImageButton1.x","27");
            postDatas.put("UserLogin:ImageButton1.y","5");
            try {
            postDatas.put("UserLogin:ddlPerson", URLEncoder.encode("卡户","GBK"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String parma = "";

        for (Iterator<Map.Entry<String,String>> it = postDatas.entrySet().iterator(); it.hasNext();){
            Map.Entry entry = it.next();
            parma += entry.getKey() + "=" + entry.getValue();
            if(it.hasNext())
                parma += "&";
        }
        final String replace = parma.replace(":", "%3a").replace("+","%2b");

        Log.d("postdatas",replace);

        new Thread(){
            @Override
            public void run() {

                RequestBody requestBody = null;
                try {
                    requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=gb2312"),new String(replace.getBytes(),"GBK"));
                    Request request = new Request.Builder().url(LOGIN_URL).removeHeader("User-Agent")
                            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
                            .post(requestBody).build();
                    Response rsp = okHttpClient.newCall(request).execute();
                    String data = rsp.body().string();
                    Log.d("code",code.getText().toString());
                    Log.d("code__",MyCode.getInstance().getCode());
                    if (rsp.code() == 302 && data != null) {
                        Log.d("data", data);
                        if (data.contains("Cardholder")) {
                            SharedPreferences.Editor editor = getSharedPreferences("userInfoData", MODE_PRIVATE).edit();
                            editor.putString("userName", userName.getText().toString());
                            editor.putString("passWord", passWord.getText().toString());
                            editor.apply();
                            getItem();

                        }
                    } else {
                        Looper.prepare();
                        myDialog.cancel();
                        Toast.makeText(context, "用户名或者密码错误，请重试", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (SocketTimeoutException e1){
                    Looper.prepare();
                    Toast.makeText(context, "连接超时，请重试", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

                        protected void getImage(){
                            new Thread(){
                                @Override
                                public void run() {
                                    Request request = new Request.Builder().url(LOGIN_URL).get().build();
                                    try {
                                        Response rsp = okHttpClient.newCall(request).execute();
                                        if (rsp.isSuccessful()){

                        String data = rsp.body().string();
                        Document document = Jsoup.parse(data);

                        Element viewstate =
                                document.select("input[name=\"__VIEWSTATE\"]").first();
                        Element eventvalidation =
                                document.select("input[name=\"__EVENTVALIDATION\"]").first();

                        String UserLogin_ImgFirst =
                                document.getElementById("UserLogin_ImgFirst").attr("src");
                        String UserLogin_imgSecond =
                                document.getElementById("UserLogin_imgSecond").attr("src");
                        String UserLogin_imgThird =
                                document.getElementById("UserLogin_imgThird").attr("src");
                        String UserLogin_imgFour =
                                document.getElementById("UserLogin_imgFour").attr("src");

                        String num = UserLogin_ImgFirst+UserLogin_imgSecond+UserLogin_imgThird+UserLogin_imgFour;

                        String regEx="[^0-9]";
                        Pattern p = Pattern.compile(regEx);
                        Matcher m = p.matcher(num);
                        Log.d("num",num);
                        Log.d("num",m.replaceAll(""));
                        Bitmap login_code = MyCode.getInstance().createBitmap(m.replaceAll(""));

                        postDatas.put("__VIEWSTATE",viewstate.attr("value"));
                        postDatas.put("__EVENTVALIDATION",eventvalidation.attr("value"));

                        Message msg = new Message();
                        msg.what = 0x1234;
                        msg.obj = login_code;
                        handler.sendMessage(msg);

                    }
                }  catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void getItem(){
        String url = "http://www.cqcet.edu.cn/";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request res = new Request.Builder().url(url+"index/xwdt.htm").get().build();
        try {
            Response rsp = okHttpClient.newCall(res).execute();
            if (rsp.isSuccessful()) {
                String data = rsp.body().string();
                Document doc = Jsoup.parse(data, "GBK");
                String pageNum = doc.getElementById("fanye50592").text().split("/")[1].replace(" ","");   //获取页数
                Log.d("pageNum",pageNum);
                Elements htmlItems = doc.getElementsByClass("c50592");
                for (int i = 0;i < htmlItems.size();i++){
                    Element element = htmlItems.get(i);
                    String next_url = element.attr("href").replace("../../","");

                    res = new Request.Builder().url(url+next_url).get().build();
                    rsp = okHttpClient.newCall(res).execute();
                    String body = rsp.body().string();
                    doc = Jsoup.parse(body, "GBK");

                    Page p = new Page();
                    p.setUrl(url+next_url);
                    p.setBody(body);
                    String page_id = next_url.split("/")[2].split("\\.")[0];
                    String num_url= "http://www.cqcet.edu.cn/system/resource/code/news/click/dynclicks.jsp?clickid="+page_id+"&owner=1135322561&clicktype=wbnews";
                    res = new Request.Builder().url(num_url).get().build();
                    rsp = okHttpClient.newCall(res).execute();

                    p.setTitle(element.attr("title"));
                    Log.d("title","title"+element.attr("title"));
                    p.setDate(doc.getElementById("article_extinfo").text().replace("]",rsp.body().string()+"]"));

                    Elements imgs = doc.select("div#article_body").select("img");
                    if(imgs.size()>0) {
                        String imgUrl = imgs.get(0).attr("src").replace("../../", url);
                        p.setImgUrl(imgUrl);
                        /*res = new Request.Builder().url(imgUrl).get().build();
                        rsp = okHttpClient.newCall(res).execute();
                        byte[] bytes = rsp.body().bytes();
                        Bitmap btm = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                        p.setImg(btm);*/
                    }

                    items.add(p);
                    //p.setImg();
                }
                //myDialog.cancel();
                Intent intent = new Intent(context, MainActivity.class);
                intent.putExtra("pageNum",Integer.parseInt(pageNum));
                intent.putExtra("items", (Serializable) items);
                myDialog.dismiss();
                startActivity(intent);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            long time = System.currentTimeMillis();
            if(time-firstTime>2000){
                Toast.makeText(context,"再按一次返回键,退出程序",Toast.LENGTH_SHORT).show();
                firstTime = time;
            }else{
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

}
